#ifndef _STORE_HPP
#define _STORE_HPP

template <class T, int length = 5>
struct Cycle_Queue
{
    int front = 0, rear = 0;
    T node[length + 1];
    bool busy[length + 1];

    Cycle_Queue(){memset(busy, 0, sizeof(busy));}

    void clear()
    {
        front = rear = 0;
        memset(busy, 0, sizeof(busy));
    }

    int get_next(int now) 
    {
        return (now + 1) >= (length + 1) ? 0 : now + 1;
    }

    int get_pre(int now)
    {
        return (now - 1) < 0 ? length : now - 1;
    }
    
    bool empty()
    {
        return front == rear;
    }

    bool full()
    {
        return get_next(rear) == front;
    }

    int push(T newnode)
    {
        if (full())
        {
            puts("queue push error : full");
            exit(0);
        }

        node[rear = get_next(rear)] = newnode;
        node[rear].pos = rear;
        busy[rear] = 1;
        return rear;
    }

    void pop()
    {
        if (empty())
        {
            puts("queue pop error : empty");
            exit(0);
        }

        front = get_next(front);
        busy[front] = 0;
    }

    void pop_rear()
    {
        if (empty())
        {
            puts("queue pop_rear error : empty");
            exit(0);
        }

        busy[rear] = 0;
        rear = get_pre(rear);
    }

    T& get_rear()
    {
        if (empty())
        {
            puts("queue get_rear error : empty");
            exit(0);
        }

        return node[rear];
    }

    T& get_front()
    {
        if (empty())
        {
            puts("queue get_front error : empty");
            exit(0);
        }

        return node[get_next(front)];
    }

    T& get_sec()
    {
        if (empty() || get_next(front) == rear)
        {
            puts("queue get_sec error : size < 2");
            exit(0);
        }
        return node[get_next(get_next(front))];
    }

    int next_pos()
    {
        return get_next(rear);
    }
};

template <class T, int length = 5>
struct Array
{
    T node[length];
    bool busy[length];
    int size = 0;

    Array(){memset(busy, 0, sizeof(busy));}

    void clear()
    {
        size = 0;
        memset(busy, 0, sizeof(busy));
    }
    
    bool empty()
    {
        return size == 0;
    }

    bool full()
    {
        return size == length;
    }

    int push(T newnode)
    {
        if (full())
        {
            puts("array push error : full");
            exit(0);
        }

        for (int i = 0; i < length; i++)
            if (!busy[i])
            {
                busy[i] = 1;
                node[i] = newnode;
                size++;
                return i;
            }
            
        return 0;
    }

    void pop(int pos)
    {
        if (empty())
        {
            puts("array pop error : empty");
            exit(0);
        }
        
        busy[pos] = 0;
        size--;
    }
};

#endif

